package com.yanqu.road.dao.impl.activity.pawnshop;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.pawnshop.GlobalPawnshopData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class CrossPawnshopTriggerDaoImpl extends TempDataObjectDao<GlobalPawnshopData> {
    public static String getGlobalPawnshopDataSql = "select * from `t_c_activity_pawnshop_trigger` where `activityId`=?";
    public GlobalPawnshopData getGlobalPawnshopData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getGlobalPawnshopDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateGlobalPawnshopDataSql = "update `t_c_activity_pawnshop_trigger` set dayTime=?,dayPower=?,dayMarkTimes = ? where activityId=?";
    @Override
    protected boolean updateData(GlobalPawnshopData globalPawnshopData) {
        globalPawnshopData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, globalPawnshopData.getDayTime()));
        para.put(i++, new DbParameter(Types.INTEGER, globalPawnshopData.getDayPower()));
        para.put(i++, new DbParameter(Types.INTEGER, globalPawnshopData.getDayMarkTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, globalPawnshopData.getActivityId()));
        if (getDb().executeNoneQuery(updateGlobalPawnshopDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGlobalPawnshopDataSql = "insert into `t_c_activity_pawnshop_trigger`(activityId,dayTime,dayPower,dayMarkTimes) values (?,?,?,?)";
    @Override
    protected boolean addData(GlobalPawnshopData globalPawnshopData) {
        globalPawnshopData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, globalPawnshopData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, globalPawnshopData.getDayTime()));
        para.put(i++, new DbParameter(Types.INTEGER, globalPawnshopData.getDayPower()));
        para.put(i++, new DbParameter(Types.INTEGER, globalPawnshopData.getDayMarkTimes()));
        if (getDb().executeNoneQuery(insertGlobalPawnshopDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GlobalPawnshopData parseData(ResultSet rs) throws SQLException {
        GlobalPawnshopData globalPawnshopData = new GlobalPawnshopData();
        globalPawnshopData.setActivityId(rs.getInt("activityId"));
        globalPawnshopData.setDayTime(rs.getLong("dayTime"));
        globalPawnshopData.setDayPower(rs.getInt("dayPower"));
        globalPawnshopData.setDayMarkTimes(rs.getInt("dayMarkTimes"));
        return globalPawnshopData;
    }

}
